/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.aaron.platform.service;

import com.aaron.platform.dao.IOderInfoDao;
import com.aaron.platform.dao.PageInfo;
import com.aaron.platform.entity.GczxOrderInfo;
import com.aaron.platform.exceptions.PayInfoExistException;
import java.util.List;
import java.util.Properties;
import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;

/**
 *
 * @author Administrator
 */
public class OrderMgrImp implements IOrderMgr {

    private IOderInfoDao orderInfoDao;

    public void setOrderInfoDao(IOderInfoDao orderInfoDao) {
        this.orderInfoDao = orderInfoDao;
    }

    public GczxOrderInfo findById(Integer orderId) {
        return orderInfoDao.findById(orderId);
    }

    public List<GczxOrderInfo> findByCode(String orderCode) {
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(GczxOrderInfo.class);
        detachedCriteria = detachedCriteria.add(Expression.eq(GczxOrderInfo.ORDER_CODE, orderCode));
        return orderInfoDao.findByCriteria(detachedCriteria);
    }

    public List<GczxOrderInfo> findByContactCode(String contactCode) {
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(GczxOrderInfo.class);
        detachedCriteria = detachedCriteria.add(Expression.ilike(GczxOrderInfo.CONTACT_CODE,
                contactCode, MatchMode.ANYWHERE));
        return orderInfoDao.findByCriteria(detachedCriteria);
    }

    public PageInfo findByCondition(Properties condition, int startIndex, int pageSize) {
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(GczxOrderInfo.class);
        if (condition != null && !condition.isEmpty()) {
            // project code
            String projectCode = condition.getProperty(GczxOrderInfo.PROJECT_CODE);
            if (projectCode != null && StringUtils.isNotEmpty(projectCode)) {
                detachedCriteria = detachedCriteria.add(Expression.eq(GczxOrderInfo.PROJECT_CODE,
                        projectCode));
            }
            // order code
            String orderCode = condition.getProperty(GczxOrderInfo.ORDER_CODE);
            if (orderCode != null && StringUtils.isNotEmpty(orderCode)) {
                detachedCriteria = detachedCriteria.add(Expression.eq(GczxOrderInfo.ORDER_CODE,
                        orderCode));
            }
             // order code
            String contactCode = condition.getProperty(GczxOrderInfo.CONTACT_CODE);
            if (contactCode != null && StringUtils.isNotEmpty(contactCode)) {
                detachedCriteria = detachedCriteria.add(Expression.eq(GczxOrderInfo.CONTACT_CODE,
                        contactCode));
            }
            // order pro
            int orderPro = 0;
            if (condition.getProperty(GczxOrderInfo.ORDER_PRO) != null) {
                orderPro = Integer.valueOf(condition.getProperty(GczxOrderInfo.ORDER_PRO));
                detachedCriteria = detachedCriteria.add(Expression.eq(GczxOrderInfo.ORDER_PRO,
                        orderPro));
            }

            // order code
            int orderStatus = 0;
            if (condition.getProperty(GczxOrderInfo.ORDER_STATUS) != null) {
                orderStatus = Integer.valueOf(condition.getProperty(GczxOrderInfo.ORDER_STATUS));
                 detachedCriteria = detachedCriteria.add(Expression.eq(GczxOrderInfo.ORDER_STATUS,
                        orderStatus));
            }

            // task name
            String taskName = condition.getProperty(GczxOrderInfo.TASK_NAME);
            if (taskName != null && StringUtils.isNotEmpty(taskName)) {
                detachedCriteria = detachedCriteria.add(Expression.ilike(GczxOrderInfo.TASK_NAME,
                        taskName, MatchMode.ANYWHERE));
            }

            // task code
            String taskCode = condition.getProperty(GczxOrderInfo.TASK_CODE);
            if (taskCode != null && StringUtils.isNotEmpty(taskCode)) {
                detachedCriteria = detachedCriteria.add(Expression.eq(GczxOrderInfo.TASK_CODE,
                        taskCode));
            }
        }

        // 排序
        detachedCriteria.addOrder(Order.asc(GczxOrderInfo.ORDER_ID));

        return orderInfoDao.findPageByCriteria(detachedCriteria,  startIndex, pageSize);
    }

    public void addOrderInfo(GczxOrderInfo gczxOrderInfo) {
        orderInfoDao.save(gczxOrderInfo);
    }

    public void updateOrderInfo(GczxOrderInfo gczxOrderInfo) {
        orderInfoDao.update(gczxOrderInfo);
    }

    public void removeOrderInfo(Integer orderId) throws PayInfoExistException {
       orderInfoDao.removeById(orderId);
    }

    public void removeOrderInfo(GczxOrderInfo gczxOrderInfo) throws PayInfoExistException {
        orderInfoDao.remove(gczxOrderInfo);
    }

    public List<GczxOrderInfo> findByCondition(Properties condition) {
        DetachedCriteria detachedCriteria = null;
        if (!condition.isEmpty()) {
            detachedCriteria = DetachedCriteria.forClass(GczxOrderInfo.class);
            // project code
            String projectCode = condition.getProperty(GczxOrderInfo.PROJECT_CODE);
            if (projectCode != null && StringUtils.isNotEmpty(projectCode)) {
                detachedCriteria = detachedCriteria.add(Expression.eq(GczxOrderInfo.PROJECT_CODE,
                        projectCode));
            }
            // order code
            String orderCode = condition.getProperty(GczxOrderInfo.ORDER_CODE);
            if (orderCode != null && StringUtils.isNotEmpty(orderCode)) {
                detachedCriteria = detachedCriteria.add(Expression.eq(GczxOrderInfo.ORDER_CODE,
                        orderCode));
            }
             // order code
            String contactCode = condition.getProperty(GczxOrderInfo.CONTACT_CODE);
            if (contactCode != null && StringUtils.isNotEmpty(contactCode)) {
                detachedCriteria = detachedCriteria.add(Expression.eq(GczxOrderInfo.CONTACT_CODE,
                        contactCode));
            }
            // order pro
            int orderPro = 0;
            if (condition.getProperty(GczxOrderInfo.ORDER_PRO) != null) {
                orderPro = Integer.valueOf(condition.getProperty(GczxOrderInfo.ORDER_PRO));
                detachedCriteria = detachedCriteria.add(Expression.eq(GczxOrderInfo.ORDER_PRO,
                        orderPro));
            }

            // order code
            int orderStatus = 0;
            if (condition.getProperty(GczxOrderInfo.ORDER_STATUS) != null) {
                orderStatus = Integer.valueOf(condition.getProperty(GczxOrderInfo.ORDER_STATUS));
                detachedCriteria = detachedCriteria.add(Expression.eq(GczxOrderInfo.ORDER_STATUS,
                        orderStatus));
            }

            // task name
            String taskName = condition.getProperty(GczxOrderInfo.TASK_NAME);
            if (taskName != null && StringUtils.isNotEmpty(taskName)) {
                detachedCriteria = detachedCriteria.add(Expression.ilike(GczxOrderInfo.TASK_NAME,
                        taskName, MatchMode.ANYWHERE));
            }

            // task code
            String taskCode = condition.getProperty(GczxOrderInfo.TASK_CODE);
            if (taskCode != null && StringUtils.isNotEmpty(taskCode)) {
                detachedCriteria = detachedCriteria.add(Expression.eq(GczxOrderInfo.TASK_CODE,
                        taskCode));
            }
        }
        if (detachedCriteria != null) {
            return orderInfoDao.findByCriteria(detachedCriteria);
        } else {
            return orderInfoDao.FindAll();
        }

    }

    public void addOrUpdateOrderInfos(List<GczxOrderInfo> gczxOrderInfos) {
        orderInfoDao.saveOrUpdateAll(gczxOrderInfos);
    }

    public void mergeOrderInfo(GczxOrderInfo gczxOrderInfo) {
        orderInfoDao.merge(gczxOrderInfo);
    }

    public boolean isSameOrderExist(GczxOrderInfo doubtedOrderInfo) {
        Properties properties = new Properties();
        properties.setProperty(GczxOrderInfo.CONTACT_CODE, doubtedOrderInfo.getContactCode());
        properties.setProperty(GczxOrderInfo.TASK_CODE, doubtedOrderInfo.getTaskCode());
        properties.setProperty(GczxOrderInfo.PROJECT_CODE, doubtedOrderInfo.getProjectCode());
        List<GczxOrderInfo> existedOrderInfos = findByCondition(properties);
        if (existedOrderInfos != null && existedOrderInfos.size() > 0) {
            if (doubtedOrderInfo.getOrderId() == null || doubtedOrderInfo.getOrderId() == 0) {
                return true;
            }
            for (GczxOrderInfo orderInfo : existedOrderInfos) {
                if (!StringUtils.equals(orderInfo.getOrderId().toString(),
                        doubtedOrderInfo.getOrderId().toString())) {
                    return true;
                }
            }
        }
        return false;
    }
}
